﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Mimoza.ObjectLayer;
using Mimoza.Apps.Console.ViewModel;

namespace Mimoza.Apps.Console.Commands
{
    class DeleteUpdateCommand : BaseConsoleCommand
    {
        UpdateViewModel m_viewModel = null;
        public DeleteUpdateCommand(UpdateViewModel viewModel)
        {
            m_viewModel = viewModel;
            m_viewModel.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(this.viewModel_PropertyChanged);
        }

        void viewModel_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (m_viewModel.Visible)
            {
                this.Visible = true;
                this.Enabled = (null != m_viewModel.Context);
            }
            else
            {
                this.Visible = false;
                this.Enabled = false;
            }
        }

        public override void Execute()
        {
            Update update = m_viewModel.Context as Update;
            if (null != update)
            {
                try
                {
                    m_viewModel.Application.ObjectLayer.UpdateRepository.Delete(update);
                    m_viewModel.Context = null;
                }
                catch (Exception e)
                {
                    string error = "Command: DeleteUpdateCommand error - '" + e.ToString() + "'.";
                    MessageBox.Show(null, error, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    Mimoza.Common.Logger.Log.Error(error);
                }
            }
        }
    }
}
